package com.webproject.service.impl.RateManager;

import java.util.Date;
import java.util.List;

import com.webproject.core.Page;
import com.webproject.dao.RateManager.RateManagerDao;
import com.webproject.entity.rate.MonthRate;
import com.webproject.entity.rate.WaterElectricityRate;
import com.webproject.service.RateManager.RateManagerService;

/**
 * @author Administrator
 *
 */
public class RateManagerServiceImpl implements RateManagerService{

	private RateManagerDao rateManagerDao;
	public RateManagerDao getRateManagerDao() {
		return rateManagerDao;
	}

	public void setRateManagerDao(RateManagerDao rateManagerDao) {
		this.rateManagerDao = rateManagerDao;
	}

	@Override
	public void queryWaterEle(Page page) {
		// TODO Auto-generated method stub
		page.setRows(rateManagerDao.queryWaterEle(page));
		page.setRecords(rateManagerDao.queryWaterEleCount(page));
	}

	@Override
	public void saveWaterEle(WaterElectricityRate rate) {
		// TODO Auto-generated method stub
		rateManagerDao.saveWaterEle(rate);
	}

	@Override
	public void updateWaterEle(WaterElectricityRate rate) {
		// TODO Auto-generated method stub
		rateManagerDao.updateWaterEle(rate);
	}

	@Override
	public void queryMonthRate(Page page) {
		// TODO Auto-generated method stub
		page.setRows(rateManagerDao.queryMonthRate(page));
		page.setRecords(rateManagerDao.queryMonthRateCount(page));
	}

	@Override
	public void saveMonthRate(MonthRate rate) {
		// TODO Auto-generated method stub
		countRate(rate);
		rateManagerDao.saveMonthRate(rate);
	}

	@Override
	public void updateMonthRate(MonthRate rate) {
		// TODO Auto-generated method stub
		countRate(rate);
		rateManagerDao.updateMonthRate(rate);
	}
	
	public void countRate(MonthRate rate){
		List<WaterElectricityRate> electricityRates=rateManagerDao.queryWaterEleByCondition(rate);
		if(electricityRates !=null && electricityRates.size()!=0){
			WaterElectricityRate rate2=electricityRates.get(0);
			float useWater=rate2.geteMonthWater()-rate2.getbMonthWater();
			rate.setUseWaterAmount(useWater);
			rate.setWaterSum(rate.getWaterPrice()*useWater);
			float useEle=rate2.geteMonthElectric()-rate2.getbMonthElectric();
			rate.setUseElectricAmount(useEle);
			rate.setElectricSum(rate.getElectricPrice()*useEle);
			rate.setWarmRentSum(rate.getWarmRentPrice()*rate2.getRoomArea());
		}
		rate.setRentSum(rate.getWaterSum()+rate.getElectricSum()+rate.getWarmRentSum());
	}


	@Override
	public boolean canSaveWaterEle(WaterElectricityRate rate, String flag) {
		// TODO Auto-generated method stub
		WaterElectricityRate rate1=rate;
		List<WaterElectricityRate> list=rateManagerDao.querySavedWaterEle(rate);
		if(flag.equals("add")){
			if(list.size()==0){
				return true;
			}else{
				return false;
			}
		}else if(flag.equals("edit")){
			for(int i=0;i<list.size();i++){
				WaterElectricityRate rate2=list.get(i);
				if(!rate1.getId().equals(rate2.getId())){
					return false;
				}
			}
			return true;
		}
		return false;
	}
}
